// pages/find_event/find_event.js
import { request } from "../../request/index.js"
import { showToast,navigateTo } from "../../utils/asyncWx.js"

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // openid: "",
    eventid: "ocansdfasdcnklsjnlkb",
    opengid: ""
  },

  /**
   * 生命周期函数--监听页面加载
   * 使用钩子技术，保证该函数在User的openid有值以后再执行，即app.js的login执行完毕后执行，保证代码同步
   */
  onUserLoad(options) {
    console.log("find_event option>>>>", options)
    let that = this
    wx.showShareMenu({
      withShareTicket: true
    })
    this.getShareTicket()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(res) {
    console.log(res);
    let { eventid } = res.target.dataset.info;

    return {
      title: '分享',
      path: '/pages/signup/signup?eventid=' + eventid,
      imageUrl: 'https://pic.imgdb.cn/item/60742ebb8322e6675cb3b97f.jpg'
    }
  },
  // 用户从群聊的小程序卡片进入，需要获取群的opengid
  async getShareTicket() {
    let that = this
    // 全局函数getShareTicket，执行了前端和后端的login、前端getShareInfo获取加密信息、后端解密，最终得到opengid
    // 注意，必须在getShareTicket的自定义函数参数中定义在获取opengid后的操作，否则获取opengid一定是最后执行的
    app.getShareTicket(async function (globalData) {
      console.log('clickReload---globalData-->', globalData)
      that.setData({
        opengid: globalData.opengid,
        eventid: globalData.eventid,
        // openid: globalData.openid
      })
      await that.bindOpenidAndOpengid()
      await that.bindOpengidAndEvevntid()
      await that.bindOpenidAndEventid()
    })
  },
  // 将openid和opengid进行绑定
  async bindOpenidAndOpengid() {
    // 将openid和opengid绑定，将opengid和eventid绑定
    // let openid = this.data.openid
    // getOpenid((res) => { openid = res })
    // console.log("openid: >>>>> ", openid)
    let { opengid } = this.data
    let header = { "Content-Type": "application/x-www-form-urlencoded" }
    const res = await request({ url: "/WxGroup/addOpengid", method: "POST", data: { opengid }, header })
    if (res.data.code == 200) {
      console.log("绑定成功")
    } else if (res.data.code == 606) {
      console.log(res.data.msg) // 返回提示信息
    }
  },
  // 将opengid和eventid进行绑定
  async bindOpengidAndEvevntid() {
    let { opengid, eventid } = this.data
    let data = { opengid, eventid }
    let header = { "Content-Type": "application/x-www-form-urlencoded" }
    const res = await request({ url: "/AttendanceEvent/updateEvent", method: "POST", data, header })
    console.log("/AttendanceEvent/updateEvent >>>>>> ", res)
  },
  // 将用户和事件进行绑定  自动绑定，根据opengid和evnetid的匹配进行绑定
  async bindOpenidAndEventid() {
    // 向后端请求将用户添加到该打卡事件中
    let { eventid } = this.data
    // let openid = this.data.openid
    let opengid = this.data.opengid
    // getOpenid((res) => { openid = res })
    let data = { eventid, opengid } // opengid用于后端判断该用户是否为该群成员
    let header = { "Content-Type": "application/x-www-form-urlencoded" }
    const res = await request({ url: "/EventUser/createEventUser", method: "POST", data, header })




    console.log("/EventUser/createEventUser >>>>>> ", res)
    // 后端验证opengid和eventid是否匹配
    if (res.data.code == 200) { // eventUser创建成功，即学生和事件绑定成功
      // 跳转到打卡页面
      let url = "/pages/signup/signup?eventid="+ eventid + "&opengid=" + opengid
      navigateTo({url})
    } else {
      showToast({ title: res.data.msg })
    }
  }
})